﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class frm_TDCT : Form
    {
        string temp = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public frm_TDCT()
        {
            InitializeComponent();
        }

        private void frm_TDCT_Load(object sender, EventArgs e)
        {
            Load_TDCT();
        }
        public void Load_TDCT()
        {
            dataGridView_TDCT.DataSource = sv.getTDCT();
            btn_Sua_TDCT.Enabled = true;
            btn_Xoa_TDCT.Enabled = true;
        }

        private void dataGridView_TDCT_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_Stt_TD_CT.Text = dataGridView_TDCT.CurrentRow.Cells[0].Value.ToString();
            txt_Ten_TDCT.Text = dataGridView_TDCT.CurrentRow.Cells[1].Value.ToString();
            temp = dataGridView_TDCT.CurrentRow.Cells[0].Value.ToString();
            txt_Stt_TD_CT.Enabled = false;
            txt_Ten_TDCT.Enabled = false;
            btn_Huy_TDCT.Enabled = false;
            btn_Luu_TDCT.Enabled = false;
        }

        private void btn_Them_TDCT_Click(object sender, EventArgs e)
        {
            flag = "them";
            btn_Huy_TDCT.Enabled = true;
            btn_Luu_TDCT.Enabled = true;
            btn_Sua_TDCT.Enabled = false;
            btn_Them_TDCT.Enabled = true;
            btn_Thoat_TDCT.Enabled = true;
            btn_Xoa_TDCT.Enabled = false;
            txt_Stt_TD_CT.Enabled = true;
            txt_Ten_TDCT.Enabled = true;
            txt_Stt_TD_CT.Text = "";
            txt_Ten_TDCT.Text = "";
        }

        private void btn_Luu_TDCT_Click(object sender, EventArgs e)
        {
            string a = txt_Stt_TD_CT.Text;
            string b = txt_Ten_TDCT.Text;
            ServiceReference1.TRINHDO_CHINHTRI tdct = new ServiceReference1.TRINHDO_CHINHTRI();
            tdct.Stt_TD_CT = Convert.ToInt32(a);
            tdct.TenTDCT = b;
            if (a == "" | b == "")
            {
                MessageBox.Show("Xin nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if(sv.check_tdct(tdct))
                        kq = -1;
                    else
                        kq = sv.addTDCT(tdct);
                }
                else
                {

                    if (flag == "sua")
                    {
                        sv.setTDCT(tdct);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_TDCT();
                }
                else
                {
                    MessageBox.Show("Lỗi!!!");
                }
            }
        }

        private void btn_Huy_TDCT_Click(object sender, EventArgs e)
        {
            btn_Sua_TDCT.Enabled = true;
            btn_Xoa_TDCT.Enabled = true;
        }

        private void btn_Sua_TDCT_Click(object sender, EventArgs e)
        {
            flag = "sua";
            txt_Ten_TDCT.Enabled = true;
            btn_Huy_TDCT.Enabled = true;
            btn_Luu_TDCT.Enabled = true;
        }

        private void btn_Xoa_TDCT_Click(object sender, EventArgs e)
        {
            ServiceReference1.TRINHDO_CHINHTRI tdct = new ServiceReference1.TRINHDO_CHINHTRI();
            tdct.Stt_TD_CT = Convert.ToInt32(temp);
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dòng này?","warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteTDCT(tdct);
                Load_TDCT();
                MessageBox.Show("Đã xóa!");
                txt_Stt_TD_CT.Text = "";
                txt_Ten_TDCT.Text = "";
            }
        }

        private void btn_Thoat_TDCT_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn thoát bảng này?", "warnring", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                this.Close();
            }
        }
    }
}
